/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @param {number} targetSum
 * @return {number[][]}
 */
var pathSum = function (root, targetSum) {
  var res = [], dfs = (node, total, nums) => {
    total += node.val, nums.push(node.val)
    if (node.left || node.right) {
      node.left && dfs(node.left, total, nums.slice())
      node.right && dfs(node.right, total, nums.slice())
    } else if (total === sum) {
      res.push(nums)
    }
  }
  return root && dfs(root, 0, []) || res
};